package com.adkun.community.config;

import com.adkun.community.quartz.AlphaJob;
import com.adkun.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * 配置 -> 数据库 -> 调用
 */
@Configuration
public class QuartzConfig {

    /**
     * BeanFactory是容器的顶层接口，而
     * Spring底层有很多XXXFactoryBean，
     * FactoryBean可简化Bean的实例化过程，如JobDetailFactoryBean可以认为它底层封装了JobDetail详细的实例化过程
     * 1. 通过FactoryBean封装了Bean的实例化过程。
     * 2. 可以将FactoryBean装配到Spring容器里
     * 3. 将FactoryBean注入其它的Bean
     * 4. 该Bean得到的是FactoryBean所管理的对象实例
     *
     * 配置JobDetail
     * @return
     */
    //@Bean
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");
        factoryBean.setGroup("alphaJobGroup");
        // 声明任务持久保存（哪怕是任务将来不运行了，触发器都没有了）
        factoryBean.setDurability(true);
        // 声明任务是不是可恢复的
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    /**
     * 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean)
     * 简单的Trigger可以实现简单的定时任务，如10分钟执行一次
     * CronTriggerFactoryBean可以用Cron表达式实现复杂的定时任务
     * @param alphaJobDetail
     * @return
     */
    //@Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000);
        // Trigger底层需要存储Job的状态
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

    /**
     * 刷新帖子分数任务
     * @return
     */
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        // 声明任务持久保存（哪怕是任务将来不运行了，触发器都没有了）
        factoryBean.setDurability(true);
        // 声明任务是不是可恢复的
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    /**
     * 定时任务触发器
     * @param postScoreRefreshJobDetail
     * @return
     */
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshJobTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 5);
        // Trigger底层需要存储Job的状态
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}
